<?php
require_once('security.php');
  
  // Counter for display
  $filename1 = "../extra/count-display.txt";
  if (file_exists($filename1)) {
    // Get the count value
    $oldcount1 = file_get_contents($filename1);
    $nextnum = ($oldcount1 + 1);
  }
  $response2 = "<br />The number of files to display in random order is currently <b>$oldcount1</b>.<br />This should not be greater than the number of <b>extra</b> files listed below.<br />Increment or decrement the number as required.<br />";

  // Increment display
  if (isset($_POST['submit_increment'])) { // Handle the form.
    $filename2 = '../extra/count-display.txt';
    $fp = fopen($filename2, "w") or die("Couldn't open $filename2.");
    $newcount1 = ($oldcount1 + 1);
    fwrite($fp, $newcount1);
    fclose($fp);
    $response2 = "<br />The count was incremented to <b>$newcount1</b>.<br />";
  }

  // Decrement display
  if (isset($_POST['submit_decrement'])) { // Handle the form.
    $filename2 = '../extra/count-display.txt';
    $fp = fopen($filename2, "w") or die("Couldn't open $filename.");
    $newcount1 = ($oldcount1 - 1);
    fwrite($fp, $newcount1);
    fclose($fp);
    $response2 = "<br />The count was decremented to <b>$newcount1</b>.<br />";
  }

  // Counter for filenames
  $filename3 = "../extra/count-filename.txt";
  if (file_exists($filename3)) {
    // Get the count value
    $oldcount2 = file_get_contents($filename3);
    $nextfile = ($oldcount2 + 1);
  }

  // New files
  if (isset($_POST['submit1'])) { // Handle the form.

    // Increment the count
    // Get the file
    $filename4 = '../extra/count-filename.txt';
    if (file_exists($filename4)) {
      // Get the count value
      $oldcount2 = file_get_contents($filename4);
    }
    // Write the incremented value
    $fp = fopen($filename4, "w") or die("Couldn't open $filename4.");
    $newcount2 = ($oldcount2 + 1);
    fwrite($fp, $newcount2);
    fclose($fp);

    // Define the content
    $content = $_POST['extra'];
    $content = stripslashes($content);

    // Create extra#.txt file
    $extrafile = 'extra' . $newcount2 . '.txt';
    if (file_exists("../extra/$extrafile")) {
      $response1 = "<br />Sorry, the filename <b>extra$newcount2.txt</b> already exists. Press <b>Add new file</b> again.<br />";
    } else {
      touch("../extra/$extrafile");
      $fp = fopen("../extra/$extrafile", "w") or die("Couldn't open $extrafile.");
      fwrite($fp, $content);
      fclose($fp);
      $response1 = "<br />The file <b>$extrafile</b> was created and the text above was added.";
    }

  }

  // Existing files
  // Retrieve the file contents
  if (isset($_POST['submit2'])) { // Handle the form.
    // Define the filename as posted
    $filename5 = $_POST['thename'];
    $showfile = $_POST['thename'];
    $filename5 = '../extra/' . $filename5 . '';
   // echo "filename" .$filename5;
    if (file_exists($filename5)) {
      // Get the content
      $content = file_get_contents($filename5);
      $response1 = "<br />The selected file is <b>$showfile</b>.";
    } else {
      $response1 = "<br />The file <b>$showfile</b> does not exist.";
    }
  }

  // Update existing file
  if (isset($_POST['submit3'])) { // Handle the form.

    // Define the content
    $content = $_POST['extra'];
    $content = stripslashes($content);
    $thename = $_POST['thename'];

    $fp = fopen("../extra/$thename", "w") or die("Couldn't open $extrafile.");
    fwrite($fp, $content);
    fclose($fp);
    $response1 = "<br />The file <b>$thename</b> was successfully edited.";
  }

?>

<?php $thisPage = "extra"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Extra</title>
<meta name="robots" content="noindex,nofollow" />
<?php require_once('theme/admintheme.php');?>
</head>
<body>

<div id="wrap">



<?php include_once('navigation.php'); ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<textarea class="textarea" name="extra" cols="70" rows="6"><?php if (isset($_POST['submit1'])) { echo stripslashes($_POST['extra']); } elseif (isset($_POST['submit2'])) { echo htmlentities(stripslashes($content)); } elseif (isset($_POST['submit3'])) { echo htmlentities(stripslashes($_POST['extra'])); } ?></textarea><br />

<b>Adding a new file</b><br />
Write the content into the box above, then press "Add a new file".<br />
The next file to be created will be <b>extra<?php echo $nextfile; ?>.txt</b>.<br />
<input type="submit" name="submit1" value="Add a new file" /><br /><br />

<b>Editing an existing file</b><br />

(1) Select the filename, then press "Get the file".<br />
<?php  
  echo '<p><b>List of extra files:</b></p>';
  $dirname = "../extra";
  $dh = opendir($dirname);
  
  echo '<select style="width:120px;" name="thename" />';
  while (false !== ($file = readdir($dh))) 
  {
    
    if (strstr($file, '.txt')&& strstr($file, 'extra')) 
    {
        if (((isset($_POST['submit2'])) || (isset($_POST['submit3']))) && ($_POST['thename']==$file))
        {
        echo '<option selected="selected" value="' . $file . '">' . $file . '</option>';
        }
        else
        {
        echo '<option value="' . $file . '">' . $file . '</option>';
        }
    }
  }
  closedir($dh);
echo '</select>';

?>

<!--input type="text" name="thename2" id="thename2" size="40" value="<?php if ((isset($_POST['submit2'])) || (isset($_POST['submit3']))) { echo $_POST['thename']; } ?>" maxlength="60" />
<br /-->
<br />
<input type="submit" name="submit2" value="Get the file" /><br />

(2) Edit the text in the main box above, then press "Save the file".<br />
<input type="submit" name="submit3" value="Save the file" /><br /><br />

<b>File display</b><br />
Increment or decrement the number of extras to display:<br />
<input type="submit" name="submit_increment" value="Increment" /> <input type="submit" name="submit_decrement" value="Decrement" />

</form>

<?php echo $response1; ?>
<?php echo $response2; ?>

<?php

  echo '<br /><p><b>List of files in extra folder:</b></p>';
  $dirname = "../extra";
  $dh = opendir($dirname);
  while (false !== ($file = readdir($dh))) {
    if (strstr($file, '.txt')) {
      print '<a href="../extra/' . $file . '" title="View">' . $file . '</a><br />';
    }
  }
  closedir($dh);

?>

</div>

</body>
</html>
